
"defer a script for domready"

from whiff.middleware import misc
from whiff import gateway

SCRIPTS_VARIABLE = "moo.DomReadyList"

class defer(misc.utility):
    def __init__(self, page,
                 variable=SCRIPTS_VARIABLE):
        self.page = page
        self.variable = variable
    def __call__(self, env, start_response):
        variable = self.param_value(self.variable, env)
        resource_path = ["local", variable]
        scripts = gateway.getResource(env, resource_path)
        #scripts = env.get(variable, [])
        javascript = self.param_value(self.page, env)
        scripts.append(javascript)
        #env[variable] = scripts
        gateway.putResource(env, resource_path, scripts)
        return self.deliver_page("", env, start_response)

__middleware__ = defer
